package com.yuqian.JavaSe.socket.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerChat {
	public static void main(String[] args) throws IOException {
		System.out.println("服务端注册服务:-------------");
		//新建serversocket，向9999端口注册服务
		ServerSocket serverSocket = new ServerSocket(9999);
		
		System.out.println("服务端等待连接:------------");
		//监听客户端的练就请求
		Socket server = serverSocket.accept();
		System.out.println("服务端连接客户端:----------");
		//String msg="客户端：你好";
		
		//System.out.println("服务端有事停了一下");
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String msg = bf.readLine();
		PrintWriter pw = new PrintWriter(server.getOutputStream());
		while(!msg.equals("exit")) {
			//这里不能用write
			pw.println(msg);
			pw.flush();
			msg  = bf.readLine();
		}
		System.out.println("服务端关闭！！！");
		//bf.close();
		pw.close();
		server.close();
		serverSocket.close();
	}
}
